{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# jupyter魔法命令"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## %run"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello python!\n"
     ]
    }
   ],
   "source": [
    "%run /Users/tiramisu/Desktop/test.py"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## %load"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello python!\n"
     ]
    }
   ],
   "source": [
    "# %load test.py\n",
    "print('hello python!') "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "import my.demo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "梓逸宸\n"
     ]
    }
   ],
   "source": [
    "my.demo.show('梓逸宸')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "from my.demo import show"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "过往云烟\n"
     ]
    }
   ],
   "source": [
    "show('过往云烟')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## %timeit"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "%timeit 计算一行命令的运算时间"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "%%timeit 计算一个代码段的运行时间"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "341 µs ± 9.32 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
     ]
    }
   ],
   "source": [
    "%timeit li = [i**2 for i in range(1000)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "368 ms ± 11.8 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)\n"
     ]
    }
   ],
   "source": [
    "%timeit li = [i**2 for i in range(1000000)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.72 µs ± 193 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n"
     ]
    }
   ],
   "source": [
    "%timeit li = [i**2 for i in range(10)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "411 µs ± 5.01 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n"
     ]
    }
   ],
   "source": [
    "%%timeit\n",
    "li = []\n",
    "for i in range(1000):\n",
    "    li.append(i**2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## %time"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "%time 无论什么代码，都只执行一遍  1 loop each"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "%%time  计算代码段执行一遍的时间"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 365 µs, sys: 0 ns, total: 365 µs\n",
      "Wall time: 371 µs\n"
     ]
    }
   ],
   "source": [
    "%time li = [i**2 for i in range(1000)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 527 µs, sys: 1e+03 ns, total: 528 µs\n",
      "Wall time: 536 µs\n"
     ]
    }
   ],
   "source": [
    "%%time\n",
    "li = []\n",
    "for i in range(1000):\n",
    "    li.append(i**2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "18.5 µs ± 509 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n"
     ]
    }
   ],
   "source": [
    "import random\n",
    "li = [random.random() for i in range(1000)]\n",
    "%timeit li.sort()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 373 µs, sys: 57 µs, total: 430 µs\n",
      "Wall time: 396 µs\n"
     ]
    }
   ],
   "source": [
    "li = [random.random() for i in range(1000)]\n",
    "%time li.sort()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "CPU times: user 30 µs, sys: 0 ns, total: 30 µs\n",
      "Wall time: 33.9 µs\n"
     ]
    }
   ],
   "source": [
    "%time li.sort()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## %%html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div class=\"mytest\" style=\"color: cyan\">梓逸宸</div>"
      ],
      "text/plain": [
       "<IPython.core.display.HTML object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%%html\n",
    "<div class=\"mytest\" style=\"color: cyan\">梓逸宸</div>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## %%js"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/javascript": [
       "document.querySelector(\".mytest\").innerHTML = \"红豆生南国\";"
      ],
      "text/plain": [
       "<IPython.core.display.Javascript object>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%%js\n",
    "document.querySelector(\".mytest\").innerHTML = \"红豆生南国\";"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## !"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "! 可以执行系统命令"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[34m$RECYCLE.BIN\u001b[m\u001b[m\r\n",
      "2018年创新创业--机票比较系统的替身\r\n",
      "\u001b[34m7.18直播截屏\u001b[m\u001b[m\r\n",
      "\u001b[34mOneDrive - csu.edu.cn\u001b[m\u001b[m\r\n",
      "Thumbs.db\r\n",
      "desktop.ini\r\n",
      "\u001b[34mhtml笔记截屏\u001b[m\u001b[m\r\n",
      "\u001b[34mjob\u001b[m\u001b[m\r\n",
      "\u001b[34mjsp\u001b[m\u001b[m\r\n",
      "me.jpeg\r\n",
      "\u001b[34mmy\u001b[m\u001b[m\r\n",
      "test.py\r\n",
      "www.runtofuture.cn.zip\r\n",
      "~$系统课设设计.docx\r\n",
      "\u001b[34m速云\u001b[m\u001b[m\r\n",
      "\u001b[34m全校st-id\u001b[m\u001b[m\r\n",
      "\u001b[34m牛客网--写简历\u001b[m\u001b[m\r\n",
      "\u001b[34m优秀文章\u001b[m\u001b[m\r\n",
      "屏幕快照 2018-06-22 下午3.03.32.png\r\n",
      "屏幕快照 2018-07-03 下午3.39.58.png\r\n",
      "屏幕快照 2018-07-07 上午9.07.46.png\r\n",
      "屏幕快照 2018-08-06 下午2.42.57.png\r\n",
      "屏幕快照 2018-08-06 下午2.43.00.png\r\n",
      "屏幕快照 2018-08-06 下午2.45.02.png\r\n",
      "屏幕快照 2018-08-06 下午2.49.00.png\r\n",
      "屏幕快照 2018-08-06 下午2.52.04.png\r\n",
      "屏幕快照 2018-08-06 下午2.52.09.png\r\n",
      "屏幕快照 2018-08-06 下午2.55.15.png\r\n",
      "屏幕快照 2018-08-06 下午2.55.48.png\r\n",
      "屏幕快照 2018-08-06 下午2.56.43.png\r\n",
      "求职技巧.pptx\r\n",
      "\u001b[34m全校部分js-id\u001b[m\u001b[m\r\n",
      "\u001b[34m桌面文件夹\u001b[m\u001b[m\r\n"
     ]
    }
   ],
   "source": [
    "! cd /Users/tiramisu/Desktop/ && ls"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## %%writefile"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Writing new_test.py\n"
     ]
    }
   ],
   "source": [
    "%%writefile new_test.py\n",
    "li = []\n",
    "for i in range(1000):\n",
    "    li.append(i**2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u001b[34mApplication Data\u001b[m\u001b[m              \u001b[34mPycharmProjects\u001b[m\u001b[m\r\n",
      "\u001b[34mApplications\u001b[m\u001b[m                  \u001b[34mQQ-cloud\u001b[m\u001b[m\r\n",
      "\u001b[34mApplications (Parallels)\u001b[m\u001b[m      \u001b[34mQt5.8.0\u001b[m\u001b[m\r\n",
      "\u001b[34mCLionProjects\u001b[m\u001b[m                 \u001b[34mQtProject\u001b[m\u001b[m\r\n",
      "\u001b[34mDesktop\u001b[m\u001b[m                       \u001b[34mSystem Volume Information\u001b[m\u001b[m\r\n",
      "\u001b[34mDocker\u001b[m\u001b[m                        \u001b[34mWorkgroup\u001b[m\u001b[m\r\n",
      "\u001b[34mDocuments\u001b[m\u001b[m                     \u001b[34mbin\u001b[m\u001b[m\r\n",
      "\u001b[34mDownloads\u001b[m\u001b[m                     \u001b[34meclipse-workspace\u001b[m\u001b[m\r\n",
      "\u001b[34mGoogle-cloud-disk\u001b[m\u001b[m             \u001b[34miCloud 云盘（归档）\u001b[m\u001b[m\r\n",
      "\u001b[34mHBuilder\u001b[m\u001b[m                      \u001b[34miCloud 云盘（归档） - 1\u001b[m\u001b[m\r\n",
      "\u001b[34mHBuilderProjects\u001b[m\u001b[m              \u001b[34miCloud 云盘（归档） - 2\u001b[m\u001b[m\r\n",
      "\u001b[34mHexo\u001b[m\u001b[m                          \u001b[34miCloud 云盘（归档） - 3\u001b[m\u001b[m\r\n",
      "\u001b[34mIdeaProjects\u001b[m\u001b[m                  jupyter_note.ipynb\r\n",
      "\u001b[34mKingsoft\u001b[m\u001b[m                      \u001b[34mmy\u001b[m\u001b[m\r\n",
      "\u001b[34mLibrary\u001b[m\u001b[m                       new_test.py\r\n",
      "\u001b[34mMovies\u001b[m\u001b[m                        \u001b[32mnodePipe1827.sock\u001b[m\u001b[m\r\n",
      "\u001b[34mMusic\u001b[m\u001b[m                         \u001b[32mnodePipe5558.sock\u001b[m\u001b[m\r\n",
      "\u001b[34mOneDrive - csu.edu.cn\u001b[m\u001b[m         \u001b[34mnode_modules\u001b[m\u001b[m\r\n",
      "\u001b[34mParallels\u001b[m\u001b[m                     \u001b[34mpython_work\u001b[m\u001b[m\r\n",
      "\u001b[34mPictures\u001b[m\u001b[m                      \u001b[34mvenv\u001b[m\u001b[m\r\n",
      "\u001b[34mPublic\u001b[m\u001b[m                        \u001b[34mwebui-aria2\u001b[m\u001b[m\r\n"
     ]
    }
   ],
   "source": [
    "! ls"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "li = []\r\n",
      "for i in range(1000):\r\n",
      "    li.append(i**2)"
     ]
    }
   ],
   "source": [
    "! cat new_test.py"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
